using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace University.Models
{ 
    public class DomainRepository : IDomainRepository
    {
        UniversityContext context = new UniversityContext();

        public IQueryable<Domain> All
        {
            get { return context.Domains; }
        }

        public IQueryable<Domain> AllIncluding(params Expression<Func<Domain, object>>[] includeProperties)
        {
            IQueryable<Domain> query = context.Domains;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public Domain Find(int id)
        {
            return context.Domains.Find(id);
        }

        public void InsertOrUpdate(Domain domain)
        {
            if (domain.DomainId == default(int)) {
                // New entity
                context.Domains.Add(domain);
            } else {
                // Existing entity
                context.Entry(domain).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var domain = context.Domains.Find(id);
            context.Domains.Remove(domain);
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose() 
        {
            context.Dispose();
        }
    }

    public interface IDomainRepository : IDisposable
    {
        IQueryable<Domain> All { get; }
        IQueryable<Domain> AllIncluding(params Expression<Func<Domain, object>>[] includeProperties);
        Domain Find(int id);
        void InsertOrUpdate(Domain domain);
        void Delete(int id);
        void Save();
    }
}